-- UILoginSetting
-- Create by cheny3 Jan/04/2016
-- 系统设置界面(登陆界面中)

UILoginSetting = class("UILoginSetting",function()
    return cc.Layer:create()
end)

function UILoginSetting.create()
    return UILoginSetting.new();
end

-- 内部函数声明
local resize;
local sortTitles;
local PER_LINE_WORD_COUNT = 21;

-- 使用本界面的项目
local NOTIFY   = 1;   -- 公告
local SYSTEM   = 2;   -- 系统设置
local LANGUAGE = 3;   -- 语言
local HELP     = 4;   -- 帮助

local TAB_NUM  = 2;   -- 标签页数量

-- 标签页界面
local firstType = {NOTIFY, SYSTEM};

-- 相关对应
local systemTypeFlag = { ["language"] = LANGUAGE, ["help"] = HELP };
local settingTypeList = { "language", "help", };

-- 语言设置内容
local languageSettingType = {};

-- 构造函数
function UILoginSetting:ctor()
    self:setName("UILoginSetting");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/setting/GameBoard.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 默认公告界面
    self.type = NOTIFY;

    -- 语言设置项相关配置
    self:initAboutLang()

    local scrollview = findChildByName(self.node, "CT/scroll_view");

    -- 容器原本的内高
    self.oriHeight = scrollview:getInnerContainerSize().height

    -- 重绘
    self:redraw();

    -- 标签页
    self:redrawTabs();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 适配
    resize(node);
end

-- 获取语言设置项相关
function UILoginSetting:initAboutLang()
    languageSettingType = LanguageM.getLanguageSettingType();
end

-- 重绘
function UILoginSetting:redraw()
    -- 标题
    local title1Label = findChildByName(self.node, "CT/title1");
    local title2Label = findChildByName(self.node, "CT/title2");

    -- 滚动条
    local systemNode = findChildByName(self.node, "CT/scroll_view");
    systemNode:setVisible(true);

    -- 去除所有子节点
    systemNode:removeAllChildren();

    -- TAB节点
    local tabNode = findChildByName(self.node, "CT/tabs");
    tabNode:setVisible(true);

    if self.indicator then
        -- 隐藏原本的滚动条
        self.indicator:setVisible(false);
        self.indicator = nil;
    end

    -- 确定按钮
    local btnOk = findChildByName(self.node, "BT/btn_more");
    btnOk:setVisible(false);

    if self.type == NOTIFY then
        -- 公告
        title1Label:setString(getLocStr("game_notify"));
        title2Label:setString(getLocStr("the_last_twenty"));
        -- 公告绘制
        self:redrawLines();
    elseif self.type == SYSTEM then
        -- 系统选择
        title1Label:setString(getLocStr("system_setting"));
        title2Label:setVisible(false);
        self:redrawItem();
    elseif self.type == LANGUAGE then
        -- 语言选择
        title1Label:setString(getLocStr("choose_language"));
        -- 当前语言
        local currentLanguage = "english";
        for _, language in pairs(languageSettingType) do
            if language == getLang() then
                currentLanguage = language;
                break;
            end
        end
        title2Label:setString(getLocStr("language_" .. currentLanguage));
        self:redrawItem();
    end

    TextStyleM.setTitleStyle(title1Label);
    TextStyleM.setSubheadStyle(title2Label);

    -- 滑动到最顶端
    scrollToTop(systemNode);
end

-- 绘制格子
function UILoginSetting:redrawItem()
    if self.type == SYSTEM then
        -- 设置
        self:initSystemItem();
    elseif self.type == LANGUAGE then
        -- 语言选择
        self:initLanguageItem();
    else
        -- 不是可以处理的类型
        return;
    end
end

-- 系统设置格子
function UILoginSetting:initSystemItem()
 local itemNode = cc.CSLoader:createNode("layout/workshop/StoreItem.csb");
    local systemNode = findChildByName(self.node, "CT/scroll_view");
    local widgetItem = replaceNodeWithWidget(itemNode);

    -- 计算容器高度
    local needHeight = math.ceil(#languageSettingType / 4) * 160 + 10;
    local totalHeight = math.max(self.oriHeight, needHeight);
    systemNode:setInnerContainerSize(cc.size(systemNode:getInnerContainerSize().width, totalHeight));

    -- 配置不允许切语言/非中文版本允许切换
    if ALLOW_TO_SWITCH_LANG ~= 1 and getLang() == "zh" then
        for index, typeName in pairs(settingTypeList) do
            if typeName == "language" then
                table.remove(settingTypeList, index);
                break;
            end
        end
    end

    -- 个人中心
    if isAccountCenterOpen() and table.indexOf(settingTypeList, "account_center") == -1 then
        table.insert(settingTypeList, "account_center");
    end

    -- UC版本关闭帮助按钮
    if PUBLISH_PLATFORM == PUBLISH_TYPE_UC then
        table.removeItem(settingTypeList, "help");
    end

    for i = 1, #settingTypeList do
        local child = widgetItem:clone();
        local textNode = ccui.Text:create();
        self:setItemPosition(child, i, textNode, totalHeight);
        child.index = settingTypeList[i];

        local icon = child:getChildByName("icon");
        local iconPath;

        if child.index == "language" then
            iconPath = getSettingIconPath("language", getLang());
        else
            iconPath = getSettingIconPath("system", child.index);
        end
        icon:loadTexture(iconPath);

        -- 注册点击事件
        self:registerItemTouchEvent(child);
        TextStyleM.setTextStyle(textNode, TextStyleM.TEXT_SIZE_MINI, TextStyleM.TEXT_COLOR_DARK_BROWN);
        if child.index == "help" then
            textNode:setString(getLocStr("response_problem"));
        else
            textNode:setString(getLocStr("system_" .. child.index));
        end

        systemNode:addChild(child);
        systemNode:addChild(textNode);
    end
end

-- 设置格子位置
function UILoginSetting:setItemPosition(child, i, textNode, totalHeight)
    local x = (i - 1) % 4 * 125 + 52;
    local y = math.floor((i - 1) / 4) * -160 + totalHeight - 60;
    child:setAnchorPoint(cc.p(0.5, 0.5));
    child:setScale(0.85);
    child:setPosition(cc.p(x, y));
    child:setVisible(true);

    textNode:setAnchorPoint(cc.p(0.5, 0.5));
    textNode:setPosition(cc.p(x, y - 75));
end

-- 语言设置格子
function UILoginSetting:initLanguageItem()
    local itemNode = cc.CSLoader:createNode("layout/workshop/StoreItem.csb");
    local systemNode = findChildByName(self.node, "CT/scroll_view");
    local widgetItem = replaceNodeWithWidget(itemNode);

    -- 计算容器高度
    local needHeight = math.ceil(#languageSettingType / 4) * 160 + 10;
    local totalHeight = math.max(self.oriHeight, needHeight);
    systemNode:setInnerContainerSize(cc.size(systemNode:getInnerContainerSize().width, totalHeight));

    for i = 1, #languageSettingType do
        local child = widgetItem:clone();
        local textNode = ccui.Text:create();
        self:setItemPosition(child, i, textNode, totalHeight);
        child.index = languageSettingType[i];

        local icon = child:getChildByName("icon");
        local iconPath;

        iconPath = getSettingIconPath("language", languageSettingType[i]);
        icon:loadTexture(iconPath);

        local focus = child:getChildByName("focus");

        local textColorType = TextStyleM.TEXT_COLOR_GRAY;

        -- 默认选中当前语言
        if child.index == getLang() then
            focus:setVisible(true);
            self.focus = child;
        else
            focus:setVisible(false);
        end

        -- 当前选择的语言字体颜色不同
        if child.index == getLang() then
            textColorType = TextStyleM.TEXT_COLOR_BLUE;
        end

        -- 注册点击事件
        self:registerItemTouchEvent(child);
        textNode:setString(getLocStr(string.format("language_%s", languageSettingType[i])));
        textNode:setTextColor(textColorType);

        -- 适应字体变细，统一放大字号后再缩小
        textNode:setFontSize(TextStyleM.TEXT_SIZE_MINI + 4);
        textNode:setScale(0.85);

        systemNode:addChild(child);
        systemNode:addChild(textNode);
    end
end

-- 绘制横条项目
function UILoginSetting:redrawLines()
    if self.type == NOTIFY then
        -- 公告
        self:redrawNotifyLines();
    else
        -- 不是可以绘制的类型
        return;
    end
end

-- 绘制Top横线
-- 为了防止滚动穿帮，先这么处理
function UILoginSetting:redrawTopLines(addNode, totalHeight)
    local lineNode = cc.CSLoader:createNode("layout/setting/NotifyNode.csb");
    local widget = replaceNodeWithWidget(lineNode);

    local child = widget:clone();
    local icon = child:getChildByName("icon");
    local text =  child:getChildByName("text");
    local bubble = child:getChildByName("bubble");
    local y = totalHeight - 20;
    child:setAnchorPoint(cc.p(0.5, 0.5));
    child:setPositionX(30);
    child:setPositionY(y);
    icon:setVisible(false);
    text:setVisible(false);
    bubble:setVisible(false);

    addNode:addChild(child);
end

-- 设置项目位置
function UILoginSetting:setLineItemPosition(child, i, totalHeight)
    local y = (i - 1) * -60 + totalHeight - 80;
    child:setAnchorPoint(cc.p(0.5, 0.5));
    child:setPositionX(30);
    child:setPositionY(y);
    child:setVisible(true);
end

-- 显示公告
function UILoginSetting:redrawBoard()
    -- 滚动容器
    local systemNode = findChildByName(self.node, "CT/scroll_view");
    -- 去除所有子节点
    systemNode:removeAllChildren();

    if self.indicator then
        -- 隐藏原本的滚动条
        self.indicator:setVisible(false);
        self.indicator = nil;
    end

    -- 标题
    local title1Label = findChildByName(self.node, "CT/title1");
    local title2Label = findChildByName(self.node, "CT/title2");
    local data = self.data;

    -- 标题1公告标题
    title1Label:setString(data["title"]);

    -- 标题2公告发布时间
    title2Label:setString(data["subhead"]);

    TextStyleM.setTitleStyle(title1Label);
    TextStyleM.setSubheadStyle(title2Label);

    -- 内容
    local contentLabel = ccui.Text:create();
    contentLabel:setAnchorPoint(0, 1);
    contentLabel:setPositionX(14);
    local contentText = data["content"];

    -- 不同语言版本的给玩家一封信需要根据字号的统一缩放增加下每行的字数
    local locFix = 1;
    if data["boardId"] == -1 or data["boardId"] == -3 then
        locFix = getLocFontSizeMulti();
    end

    TextStyleM.setTextStyle(contentLabel, TextStyleM.TEXT_SIZE_SMALL2,
        TextStyleM.TEXT_COLOR_DARK_BROWN, false, -1, nil, nil, PER_LINE_WORD_COUNT / locFix);

    -- 先赋予文字/行间距用于计算高度
    contentLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_LINE_HIGHT_MIDDLE);
    contentLabel:setString(extractString(contentText));

    -- 设置容器高度
    local scrollview = findChildByName(self.node, "CT/scroll_view");
    local height = math.max(getRealContentSize(contentLabel).height, self.oriHeight);
    contentLabel:setPositionY(height - 15);
    scrollview:setInnerContainerSize(cc.size(scrollview:getInnerContainerSize().width, height));

    -- 绘制横线
    local lineNode;
    lineNode = ccui.ImageView:create(getSettingIconPath("system", "line"));
    local linePosY = height - 10;
    while linePosY > 0 do
        local line = lineNode:clone();
        line:setPosition(250, linePosY);
        scrollview:addChild(line);
        linePosY = linePosY - TextStyleM.TEXT_LINE_HIGHT_MIDDLE;
    end

    if height > self.oriHeight then
        -- 添加滚动条
        UIIndicator.attach(scrollview);
        self.indicator = scrollview:getParent():getChildByName("UIIndicator");
        self.indicator:setPositionX(self.indicator:getPositionX() + 2);
    end

    -- 文字效果
    contentLabel = applyString(contentLabel, contentText, nil, nil, TextStyleM.TEXT_LINE_HIGHT_MIDDLE, PER_LINE_WORD_COUNT / locFix);

    scrollview:addChild(contentLabel);
end

-- 绘制公告条目
function UILoginSetting:redrawNotifyLines()
    -- 这个APP还没有登陆过账号/默认显示冈布奥的公告
    if (not UserDefaultM.getStringForKey("create_time")
        or UserDefaultM.getStringForKey("create_time") == "") then
        local board = GameBoardM.getGameBoardContentById(-1);
        self.data = {
            ["title"] = board["title"],
            ["subhead"] = board["showTime"],
            ["content"] = board["content"],
            ["boardId"] = -1,
        };
        GameBoardM.addUserReadRecord(-1);
        self:redrawBoard();
        return;
    end

    local addNode = findChildByName(self.node, "CT/scroll_view");
    local lineNode = cc.CSLoader:createNode("layout/setting/NotifyNode.csb");
    local widget = replaceNodeWithWidget(lineNode);

    -- 获取所有公告标题
    local titles = GameBoardM.getAllTitle();
    -- 获取玩家阅读公告记录
    local readRecord = GameBoardM.getUserReadRecord();

    -- 排序
    table.sort(titles, sortTitles);

    -- 计算容器高度
    local needHeight = GameBoardM.getTitleCount() * 60 + 30;
    local totalHeight = math.max(self.oriHeight, needHeight);
    addNode:setInnerContainerSize(cc.size(addNode:getInnerContainerSize().width, totalHeight));

    -- 绘制顶部横线
    if GameBoardM.getTitleCount() > 0 then
        self:redrawTopLines(addNode, totalHeight);
    end

    if totalHeight > self.oriHeight then
        -- 增加新的滚动条
        UIIndicator.attach(addNode);
        self.indicator = addNode:getParent():getChildByName("UIIndicator");
    end

    local i = 1;
    for index, m in pairs(titles) do
        local child = widget:clone();
        local icon = child:getChildByName("icon");
        local text =  child:getChildByName("text");
        local bubble = child:getChildByName("bubble");
        local panel  = child:getChildByName("panel");
        self:setLineItemPosition(child, i, totalHeight);

        -- 根据公告类型获取图标
        local iconPath = getSettingIconPath("board", tostring(m["type"]));
        local bubblePath = getSettingIconPath("system", "read");
        local oriScale = bubble:getScale();
        icon:loadTexture(iconPath);
        bubble:loadTexture(bubblePath);

        -- 设置标题
        text:setString(m["title"]);

        local color = TextStyleM.TEXT_COLOR_DARK_BROWN;
        if table.indexOf(readRecord, m["id"]) == -1 then
            -- 没有读过
            color = TextStyleM.TEXT_COLOR_GREEN;
        end
        TextStyleM.setTextStyle(text, TextStyleM.TEXT_SIZE_SMALL2, color);

        -- 打开公告界面
        local function openGameBoard()
            AudioM.playFx("button_click");
            local board = GameBoardM.getGameBoardContentById(m["id"]);
            local data = {
                ["title"] = board["title"],
                ["subhead"] = board["showTime"],
                ["content"] = board["content"],
                ["boardId"] = m["id"],
            };
            -- 弹出公告界面
            local uiGameBoard = UIGameBoard.create(data);
            UIMgr.getCurrentScene():addForm(uiGameBoard);

            self:redraw();
        end

        local function onClick(sender, eventType)
            -- 点击时的回调
            if eventType == ccui.TouchEventType.began then
                bubble:setScale(oriScale - 0.1);
            elseif eventType == ccui.TouchEventType.canceled then
                bubble:setScale(oriScale);
            elseif eventType == ccui.TouchEventType.ended then
                bubble:setScale(oriScale);
                openGameBoard();
            end
        end
        bubble:addTouchEventListener(onClick);

        local function onClick2(sender, eventType)
            -- 扩大响应区域，点击时的回调
            if eventType == ccui.TouchEventType.ended then
                openGameBoard();
            end
        end
        panel:addTouchEventListener(onClick2);

        addNode:addChild(child);
        i = i + 1;
    end
end

-- 排序：未阅读/时间高的优先
function sortTitles(item1, item2)
    -- 获取玩家阅读公告记录
    local readRecord = GameBoardM.getUserReadRecord();

    local index1 = table.indexOf(readRecord, item1["id"]);
    local index2 = table.indexOf(readRecord, item2["id"]);

    if (index1 ~= index2) and (index1 == -1 or index2 == -1) then
        return table.indexOf(readRecord, item1["id"]) < table.indexOf(readRecord, item2["id"]);
    end

    -- 按时间排序
    local createTimeA = item1["create_time"] or 1000;
    local createTimeB = item2["create_time"] or 1000;

    return createTimeA > createTimeB;
end

-- 显示帮助
function UILoginSetting:showHelp()
    -- 打开helpshift反馈问题
    local log = {
        ["mac"]              = NetworkStatus.getMacAddress(),
        ["lang"]             = getLang(),
        ["wifi_connected"]   = tostring(NetworkStatus.isWifiConnected());
        ["mobile_connected"] = tostring(NetworkStatus.isMobileConnected());
        ["dislist"]          = tableToString(_G["dislist"] or {}),
        ["error_log"]        = getErrorLog();
    };

    local brief = {
        ["lang"]     = lang,
        ["error_log"] = tableToString(log),
    };

    showFAQs(brief);
end

-- 注册项目点击回调
function UILoginSetting:registerItemTouchEvent(node)
    local btnOk = findChildByName(self.node, "BT/btn_ok");

    if self.type == SYSTEM then
        -- 语言
        local bg = node:getChildByName("bg");
        local focus = node:getChildByName("focus");

        local function onClick(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                bg:setScale(0.9);
            elseif eventType == ccui.TouchEventType.canceled then
                bg:setScale(1.0);
            elseif eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_click");
                bg:setScale(1.0);

                if node.index == "help" then
                    -- 内置客服
                    self:showHelp();
                    return;
                elseif node.index == "account_center" then
                    -- 个人中心
                    showAccountCenter();
                    return;
                end

                -- 任然使用本界面的项目，更换标记
                self.type = systemTypeFlag[node.index];

                -- 重绘页面
                self:redraw();
            end
        end
        bg:addTouchEventListener(onClick);
    elseif self.type == LANGUAGE then
        -- 语言
        local bg = node:getChildByName("bg");
        local focus = node:getChildByName("focus");

        local function onClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_click");
                local lastFocus = self.focus:getChildByName("focus");
                lastFocus:setVisible(false);
                focus:setVisible(true);
                self.focus = node;
            end
        end
        bg:addTouchEventListener(onClick);
    end
end

-- 标签页
function UILoginSetting:redrawTabs()
    local tabNode = findChildByName(self.node, "CT/tabs");

    -- 标签页数量
    local tabNum = TAB_NUM;

    -- 置灰颜色
    local blendColor = TextStyleM.TEXT_COLOR_BLEND;
    local normalColor = TextStyleM.TEXT_COLOR_NORMAL;

    -- 标签
    self.tabList = {};

    for i = 1, tabNum do
        local child = tabNode:getChildByName("tab" .. i);
        child:setName("tab" .. i);
        child.index = i;

        self.tabList[i] = child;

        self:registerTabTouchEvent(child);

        local tabLight = findChildByName(child, "light");
        local tabIcon = findChildByName(child, "icon");
        local tabImg = findChildByName(child, "img");

        if i ~= self.type then
            tabImg:setColor(blendColor);
            tabIcon:setColor(blendColor);
            tabLight:setVisible(false);
        else
            tabLight:setVisible(true);
            self.tabSelect = child;
        end
    end

    for i = 1, #self.tabList do
        local tab = self.tabList[i];
        tab:setPositionY(tab:getPositionY() + 100);
    end

    -- tab的动画效果
    performWithDelay(self, function() self:playTabEffect(); end, 0.2);
end

-- 页签动画效果
function UILoginSetting:playTabEffect()
    -- 带窗口动画播放结束后，页签从上面往下掉
    for i = 1, #self.tabList do
        local tab = self.tabList[i];
        local pos = cc.p(tab:getPosition());
        local delay = cc.DelayTime:create(0.2 * (i - 1));
        local moveDown = cc.MoveTo:create(0.2, cc.p(pos.x, pos.y - 100 - 5));
        local moveBack = cc.MoveTo:create(0.2, cc.p(pos.x, pos.y - 100));
        local action = cc.Sequence:create(delay, moveDown, moveBack);
        tab:runAction(action);
    end
end

-- 注册标签页点击回调
function UILoginSetting:registerTabTouchEvent(node)
    -- 节点
    local tabLight = node:getChildByName("light");
    local tabIcon = node:getChildByName("icon");
    local tabImg = node:getChildByName("img");

    -- 置灰颜色
    local blendColor = TextStyleM.TEXT_COLOR_BLEND;
    local normalColor = TextStyleM.TEXT_COLOR_NORMAL;

    local function onClick(sender, eventType)
        -- 点击时的回调
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 去除上一个高亮
            self.tabSelect:getChildByName("img"):setColor(blendColor);
            self.tabSelect:getChildByName("icon"):setColor(blendColor);
            self.tabSelect:getChildByName("light"):setVisible(false);

            -- 设置本身高亮
            tabLight:setVisible(true);
            tabImg:setColor(normalColor);
            tabIcon:setColor(normalColor);

            self.tabSelect = node;

            if node.index == 1 then
                self.type = NOTIFY;
            elseif node.index == 2 then
                self.type = SYSTEM;
            end

            -- 重绘页面
            self:redraw();
        end
    end

    tabIcon:addTouchEventListener(onClick);
end

-- 切换语言
function UILoginSetting:switchLanguage(selectLang)
    -- 语言设置
    local function okFunc()

        local function delayCall()
            local targetLang = "en";
            local function onSwitchLang(success, errcode)
                if success ~= true then
                    local errCodeStr = string.format(getLocStr("download_pkg_fail"), errcode);
                    alert(errCodeStr);

                    if IGNORE_DOWNLOAD_PKG_RESULT ~= 1 then
                        return;
                    end
                end

                -- 飘字提示
                alert(getLocStr("change_language_tips2"));

                -- 设置目标语言
                setLang(targetLang);
                initLang();

                local function callback()
                    if isAndroidPlatform() then
                        restartApp();
                    else
                        exitApp();
                    end
                end
                -- 延迟执行
                performWithDelay(self.node, callback, 1);
            end

            -- 任何情况下都可以切换语言，直接回到登陆界面
            local needPkg = "";
            targetLang = self.focus.index;
            -- 判断需要下载安装包的语言
            local needPkgArr = LanguageM.getNeedPkg();
            if needPkgArr[targetLang] then
                needPkg = needPkgArr[targetLang];
            end

            if needPkg ~= "" then
                downloadPkg(getLocStr("download_pkg_title"), getLocStr("download_pkg_desc"), needPkg, onSwitchLang);
            else
                onSwitchLang(true);
            end
        end
        -- 延迟重登
        performWithDelay(self.node, delayCall, 0.01);
    end

    confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("slime_note"), getLocStr("change_language_tips"),
        okFunc, nil, getLocStr("btn_text_ok"), getLocStr("btn_text_cancel"),
        { ["cancelColor"] = "blue"} );
end

-- 注册点击事件
function UILoginSetting:registerTouchEvent()
   -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            if self.type == LANGUAGE then
                -- 选择语言为当前语言，确认按钮需要置灰
                -- 不允许切换语言时，选择非简体中文，确认按钮也要置灰
                local selectLang = self.focus.index;
                if selectLang == getLang() then
                    -- 什么也不做
                    self.type = SYSTEM;
                    self:redraw();
                    return;
                else
                    if ALLOW_TO_SWITCH_LANG ~= 1 and selectLang ~= "zh" then
                        -- 只允许切换到简体中文
                        alert(getLocStr("function_is_not_open"));
                        return;
                    end

                    self:switchLanguage(selectLang);
                    return;
                end
            end
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UILoginSetting");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_ok"));
end

-- 注册事件处理回调函数
function UILoginSetting:registerEventCallback()
    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UILoginSetting 界面析构清理");
            EventMgr.removeAll("UILoginSetting");
        elseif ev == "enter" then
        end
    end);
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end